import { reactive, ref } from "vue";
import { defineStore, type DefineStoreOptionsBase, type PiniaPluginContext } from "pinia";

import {
    api_get_briefing,
    api_get_essay,
    api_get_gushici,
    api_get_Ip,
    api_get_movie_info,
    api_get_top_list,
    type Category,
    type TopListId
} from "@/api/api_everyday";
import type { Movie } from "@/model/movie";
import type { Essay } from "@/model/essay";
import type { TopList } from "@/model/topList";
import type { AncientPoetry } from "@/model/ancientPoetry";
import type { Calendar, News, Phrase, Weather } from "@/model/briefing";

export const useHomeStore = defineStore(
    "home",
    () => {
        // 电影
        const movieData = ref<Movie>();
        const getMovieData = async () => {
            const res = await api_get_movie_info();
            movieData.value = res.data;
        };

        // 文章
        const essayData = ref<Essay>();
        const getEssayData = async () => {
            const res = await api_get_essay();
            essayData.value = res.data;
        };

        // 榜单
        const topListData = reactive<Record<TopListId, TopList>>({
            WeiBo: [],
            TouTiao: [],
            TodayInHistory: [],
            ZhiHuRiBao: [],
            BiliBili: []
        });
        const getTopListData = async (topListId: TopListId = "WeiBo") => {
            const res = await api_get_top_list(topListId);
            topListData[topListId] = res.data;
        };

        // 古诗词
        const ancientPoetryData = ref<AncientPoetry>();
        const getAncientPoetryData = async () => {
            const res = await api_get_gushici();
            ancientPoetryData.value = res.data;
        };

        // 简报：新闻，天气，成语，日历
        const briefingData = reactive({
            news: [] as News[],
            weather: {} as Weather,
            calendar: {} as Calendar,
            phrase: {} as Phrase
        });
        const getBrigefingData = async (category?: Category, ip?: string, count = 20) => {
            const { data } = await api_get_briefing(ip, count, category);
            briefingData.calendar = data.calendar;
            briefingData.news = data.newsList;
            briefingData.phrase = data.phrase;
            briefingData.weather = data.weather;
        };
        // ip
        const ip = ref("");
        const getIp = async () => {
            const res = await api_get_Ip();
            ip.value = res.ip;
        };

        // 简报with天气
        const getBrigefingDataWithWeather = async () => {
            await getIp();
            await getBrigefingData(undefined, ip.value, 20);
        };

        const getHomeData = () => {
            getEssayData();
            // getMovieData()
            getTopListData();
            getAncientPoetryData();
            getBrigefingDataWithWeather();
        };
        return {
            getBrigefingData,
            briefingData,
            movieData,
            essayData,
            getHomeData,
            getMovieData,
            getEssayData,
            topListData,
            getTopListData,
            ancientPoetryData,
            getAncientPoetryData
        };
    },
    {
        persist: {
            enabled: true // true 表示开启持久化保存
        }
    } as DefineStoreOptionsBase<any, PiniaPluginContext["store"]>
);

// export const useHomeStore = defineStore('home', {
//     state: () => ({
//
//         movieData: {} as Movie
//
//     }),
//     actions: {
//         async getMovieData () {
//             const res = await api_get_movie_info()
//             this.movieData = res.data
//         }
//     },
//     persist: {
//         enabled: true // true 表示开启持久化保存
//     }
// })
